# -*- coding:utf-8 -*-

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import xlrd
from lxml import etree


"""
58同城APP-经济人
"""


class Action():
    def __init__(self):
        # 初始化配置，设置Desired Capabilities参数
        self.desired_caps = {
            "platformName": "Android",
            "deviceName": "127.0.0.1:62001",
            "appPackage": "com.wuba",
            "appActivity": "com.wuba.activity.launch.LaunchActivity",
            "udid": "127.0.0.1:62001",
            "noReset": "True",
            "newCommandTimeout": "600",
            # "unicodeKeyboard": True,
            # "resetKeyboard": True

        }
        # 指定Appium Server
        self.server = 'http://localhost:4723/wd/hub'
        # 新建一个Session
        self.driver = webdriver.Remote(self.server, self.desired_caps)
        self.driver.implicitly_wait(10)

        #获取屏幕的高
        self.x = self.driver.get_window_size()['width']
        # 获取屏幕宽
        self.y = self.driver.get_window_size()['height']


    def get_content(self):
        # # 检查搜索框
        # print("---------点击【首页-二手房】------------")
        # self.driver.find_element_by_xpath('//*[contains(@text,"二手房")]').click()
        # print("---------点【二手房】------------")#com.wuba:id/recyclerView
        # WebDriverWait(self.driver, 5).until(
        #     EC.presence_of_element_located((By.ID, "com.wuba:id/iconSdv"))).click()

        # self.swipeUp(self.driver, t=1000)
        while True:
            # 循环列表
            userlist = WebDriverWait(self.driver, 20).until(
                EC.presence_of_all_elements_located((By.ID, 'com.wuba:id/item_broker_root')))
            # userlist = self.driver.find_elements_by_xpath('//*[@resource-id="com.wuba:id/title_text_view"]')
            for u in userlist:
                try:
                    time.sleep(1)
                    dict_data = dict()
                    dict_data['name'] = u.find_element_by_id("com.wuba:id/broker_name").get_attribute("text")
                    print("---------点【列表】------------")
                    u.click()

                    ele = WebDriverWait(self.driver, 20).until(
                            EC.presence_of_element_located((By.ID, 'com.wuba:id/broker_detail_company_name')))
                    page = self.driver.page_source
                    selector = etree.XML(page.encode('utf-8'))
                    # 公司
                    dict_data['gs'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/broker_detail_company_name"]/@text')[0]
                    # 门店
                    dict_data['md'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/broker_detail_store_name"]/@text')[0]
                    # 主营商圈
                    dict_data['sq'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/broker_detail_blocks"]/@text')[0]
                    # 主营小区
                    dict_data['area'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/broker_detail_communities"]/@text')[0]
                    # 增值服务
                    dict_data['services'] = selector.xpath('//android.widget.TextView[@resource-id="com.wuba:id/broker_detail_value_service"]/@text')[0]

                    print("---------点击【电话】------------")
                    WebDriverWait(self.driver, 20).until(
                        EC.presence_of_element_located((By.ID, "com.wuba:id/call_text"))).click()

                    time.sleep(2)
                    # 点击呼叫
                    WebDriverWait(self.driver, 20).until(
                        EC.presence_of_element_located((By.ID, "android:id/button1"))).click()
                    time.sleep(2)
                    tel_element = WebDriverWait(self.driver, 20).until(
                            EC.presence_of_element_located((By.ID, "com.cootek.smartdialer:id/phonenumber_input")))
                    dict_data['tel'] = tel_element.text.replace("-", "")
                    print(str(dict_data))
                    self.driver.keyevent(4)  # 退出拨号界面
                    time.sleep(2)
                    WebDriverWait(self.driver, 20).until(
                        EC.presence_of_element_located((By.ID, "com.wuba:id/call_text")))
                    time.sleep(2)
                    self.driver.keyevent(4)  # 退出详细界面
                except:
                    # self.driver.keyevent(4)  # 返回
                    print("=========错误============")
                    time.sleep(5)
                    pass
            self.swipeUp(self.driver, t=1000)




    '''
    参数 1：driver
    参数 2：t 是持续时间
    参数 3：滑动次数
    '''
    def swipeUp(self, driver, t=500, n=1):
        '''向上滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width'] * 0.5  # x 坐标
        y1 = l['height'] * 0.75  # 起始 y 坐标
        y2 = l['height'] * 0.25  # 终点 y 坐标
        for i in range(n):
            driver.swipe(x1, y1, x1, y2, t)

    def swipeDown(self, driver, t=500, n=1):
        '''向下滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width'] * 0.5  # x 坐标
        y1 = l['height'] * 0.25  # 起始 y 坐标
        y2 = l['height'] * 0.75  # 终点 y 坐标
        for i in range(n):
            driver.swipe(x1, y1, x1, y2, 1)


    def swipLeft(self, driver, t=500, n=1):
        '''向左滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width'] * 0.75
        y1 = l['height'] * 0.5
        x2 = l['width'] * 0.05
        for i in range(n):
            driver.swipe(x1, y1, x2, y1, t)


    def swipRight(self, driver, t=500, n=1):
        '''向右滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width'] * 0.05
        y1 = l['height'] * 0.5
        x2 = l['width'] * 0.75
        for i in range(n):
            driver.swipe(x1, y1, x2, y1, t)



if __name__ == '__main__':
    action = Action()
    action.get_content()
